package com.kzw.oa.util;

import com.kzw.core.mapper.Jackson;
import com.kzw.core.orm.Page;

/**
 * 采用Jackson实现
 * */
public class JSON2 {

	private Object bean;
	private Jackson jackson = Jackson.me();

	/**
	 * 直接渲染对象
	 * */
	public JSON2(Object bean, boolean ispure) {
		if (ispure) {
			this.bean = bean;
		} else {
			this.bean = new ObjectJson(bean);
		}
	}

	public JSON2(Object bean) {
		if (!(bean instanceof Page)) {// 如果不是page的话
			bean = new ObjectJson(bean);
		}
		this.bean = bean;
	}

	public JSON2(Object bean, String dateFormater, boolean ispure) {
		jackson.setDateFormate(dateFormater);
		if (ispure) {
			this.bean = bean;
		} else {
			this.bean = new ObjectJson(bean);
		}
	}

	public JSON2(Object bean, String dateFormater) {
		jackson.setDateFormate(dateFormater);
		if (!(bean instanceof Page)) {// 如果不是page的话
			bean = new ObjectJson(bean);
		}
		this.bean = bean;
	}

	/**
	 * 只输出指定属性
	 * */
	public JSON2 with(Class<?> target, String... properties) {
		jackson.with(target, properties);
		return this;
	}
	
	public JSON2 with(Class<?>[] targets, String... properties) {
		for(Class<?> target : targets) {
			jackson.with(target, properties);
		}
		return this;
	}

	/**
	 * 不输出指定属性
	 * */
	public JSON2 without(Class<?> target, String... properties) {
		jackson.without(target, properties);
		return this;
	}
	
	public JSON2 without(Class<?>[] targets, String... properties) {
		for(Class<?> target : targets) {
			jackson.without(target, properties);
		}
		return this;
	}

	public JSON2 setDateFormate(String dateFormat) {
		jackson.setDateFormate(dateFormat);
		return this;
	}

	public String toJson() {
		return jackson.toJson(bean);
	}

}
